<?php

namespace Admin\Widget;

class TcmsWidget extends \Think\Controller
{

    /**
     * categoryID 字段
     * @param type $field
     * @return string|null
     */
    public function showCategory($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {

            $categoryModel = new \Common\Model\CategoryModel();
            $map['lm_id'] = array('EQ', $menuData['lm_id']);
            $map['deleted'] = array('EQ', 0);
            $cateData = $categoryModel->where($map)->order('sort asc')->select();
            $treeModel = new \Common\Org\Tree($cateData);
            $cateData = $treeModel->getArray();

            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<select name="' . $field . '">';
            $returnStr .= '<option value="0">----请选择----</option>';
            foreach ($cateData as $v) {
                if ($v['id'] == $data['category'])
                    $selectStr = 'selected = "selected"';
                else
                    $selectStr = '';
                $returnStr .= '<option value="' . $v['id'] . '"' . $selectStr . '>' . $v['category_title'] . '</option>';
            }
            $returnStr .= '</select>';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * categoryID 字段
     * @param type $field
     * @return string|null
     */
    public function showCategorySelect($data, $selected, $menuData)
    {
        $categoryModel = new \Common\Model\CategoryModel();
        $map['lm_id'] = array('EQ', $menuData['lm_id']);
        $map['deleted'] = array('EQ', 0);
        $cateData = $categoryModel->where($map)->order('sort asc')->select();
        $treeModel = new \Common\Org\Tree($cateData);
        $cateData = $treeModel->getArray();

        $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
        $returnStr .= '上级类别';
        $returnStr .= '</label><div class="layui-input-block">';
        $returnStr .= '<select name="parent_id">';
        $returnStr .= '<option value="0">----顶级类别----</option>';
        foreach ($cateData as $v) {
            if ($v['id'] == $data['parent_id'] || $v['id'] == $selected)
                $selectStr = 'selected = "selected"';
            else
                $selectStr = '';
            $returnStr .= '<option value="' . $v['id'] . '"' . $selectStr . '>' . $v['category_title'] . '</option>';
        }
        $returnStr .= '</select>';
        $returnStr .= '</div></div>';
        return $returnStr;

        return NULL;
    }

    /**
     * input 字段
     * @param type $field
     * @return string|null
     */
    public function showInput($field, $data, $menuData)
    {
        $selectCheckArray = explode('|', $menuData[$field . '_']);
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            if (in_array($field . '_select', $menuData['allow_field'])) {
                $returnStr .= '<select name="' . $field . '">';
                foreach ($selectCheckArray as $v) {
                    $selected = '';
                    if ($v == $data[$field])
                        $selected = 'selected';
                    $returnStr .= '<option value="' . $v . '" ' . $selected . '>' . $v . '</option>';
                }
                $returnStr .= '</select>';
            } elseif (in_array($field . '_check', $menuData['allow_field'])) {
                foreach ($selectCheckArray as $v) {
                    $checked = '';
                    $fieldArray = explode('|', $data[$field]);
                    if (in_array($v, $fieldArray))
                        $checked = 'checked';
                    $returnStr .= '<input type="checkbox" name="' . $field . '[]" title="' . $v . '" value="' . $v . '"' . $checked . ' />';
                }
            }elseif (in_array($field . '_date', $menuData['allow_field'])) {
                $returnStr .= '<input type="text" class="layui-input dataSelect" name="' . $field . '" id="' . $field . '" value="' . $data[$field] . '" style="width:274px;" />';
            } elseif (in_array($field . '_single', $menuData['allow_field'])) {
                $lmid = $menuData[$field . '_'];
                $model = new \Common\Model\NewsModel();
                $joinData = $model->where('`lm_id`=' . $lmid . ' and `deleted`=0')->select();
                $returnStr .= '<select name="' . $field . '">';
                $returnStr .= '<option value="">=========请选择' . $menuData[$field] . '=========</option>';
                foreach ($joinData as $v) {
                    $checked = '';
                    if ($data[$field] == $v['id'])
                        $checked = 'selected';
                    $returnStr .= '<option value="' . $v['id'] . '" ' . $checked . '>' . $v['title'] . '</option>';
                }
                $returnStr .= '</select>';
            } elseif (in_array($field . '_multi', $menuData['allow_field'])) {
                $lmid = $menuData[$field . '_'];
                $model = new \Common\Model\NewsModel();
                $joinData = $model->where('`lm_id`=' . $lmid . ' and `deleted`=0')->select();
                foreach ($joinData as $v) {
                    $checked = '';
                    if (in_array($v['id'], explode('|', $data[$field])))
                        $checked = 'checked';
                    $returnStr .= '<input type="checkbox" name="' . $field . '[]" value="' . $v['id'] . '" title="' . $v['title'] . '" ' . $checked . ' />';
                }
            } else {
                $returnStr .= '<input type="text" id="' . $field . '" name="' . $field . '" placeholder="请输入' . $menuData[$field] . '." value="' . $data[$field] . '" autocomplete="off" class="layui-input">';                //$returnStr .= '<input type="text" id="' . $field . '" name="' . $field . '" value="' . $data[$field] . '" size="50" />';
            }
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * Areaselect 字段
     * @param type $field
     * @return string|null
     */
    public function showAreaselect($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<tr><td width="100">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</td><td class="textleft">';
            $returnStr .= '<input type="hidden" id="hfStarProvinceID" name="province" value="' . $data['province'] . '" />';
            $returnStr .= '<input type="hidden" id="hfStarCityID" name="city" value="' . $data['city'] . '" />';
            $returnStr .= '<input type="hidden" id="hfStarDistrictID" name="area" value="' . $data['area'] . '" />';
            $returnStr .= '<div id="SelectArea"></div>';
            $returnStr .= '</td></tr>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * 排序字段 sort
     * @param type $field
     * @return string|null
     */
    public function showSort($field, $data, $menuData)
    {
        $selectCheckArray = explode('|', $menuData[$field . '_']);
        if ($menuData['sort']) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= '排序值';
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<input type="text" name="' . $field . '" value="' . $data[$field] . '" placeholder="请输入排序值." autocomplete="off" class="layui-input">';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * 文本区域
     * @param type $field
     * @return string|null
     */
    public function showTextarea($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<textarea name="' . $field . '" id="' . $field . '" placeholder="请输入' . $menuData[$field] . '" class="layui-textarea">' . $data[$field] . '</textarea>';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * 回复文本区域
     * @param type $field
     * @return string|null
     */
    public function showRecontent($field, $data, $menuData)
    {
        if (in_array('huifu', $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= '回复';
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<textarea name="' . $field . '" id="' . $field . '" placeholder="请输入回复内容...' . $menuData[$field] . '" class="layui-textarea">' . $data[$field] . '</textarea>';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * 编辑器区域
     * @return string|null
     */
    public function showEditorContent($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<textarea class="content" cols="100" rows="15" name="' . $field . '">' . stripslashes($data[$field]) . '</textarea>';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * 单选按钮
     * @return string|null
     */
    public function showRadio($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            $checkedY = '';
            $checkedN = '';
            if ($data[$field])
                $checkedY = 'checked';
            else
                $checkedN = 'checked';
            $returnStr .= '<input type="radio" name="' . $field . '" value="1" title="是" ' . $checkedY . ' /><input type="radio" name="' . $field . '" value="0" title="否"' . $checkedN . ' />';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * 单图片上传字段
     *
     */
    public function showUploadSingle($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<button type="button" class="layui-btn" id="' . $field . '">';
            $returnStr .= '<i class="layui-icon">&#xe67c;</i>选择文件</button>';
            $returnStr .= '<div class="layui-upload-list" id="' . $field . '-list-div">';

            if (is_file(APP_ROOT . $data[$field])) {
                $returnStr .= '<li><a href="' . $data[$field] . '" target="_blank"><img class="layui-list-img" id="' . $field . '-list-img" src="' . $data[$field] . '"></a><input type="hidden" name="' . $field . '" value="' . $data[$field] . '"><span class="closed"></span></li>';
            }
            $returnStr .= '</div></div></div>';

            return $returnStr;
        }
        return NULL;
    }

    /**
     * 多图片上传
     */
    public function showUploadMulti($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class = "layui-form-item"><label class = "layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class = "layui-input-block">';
            $returnStr .= '<button type = "button" class = "layui-btn" id = "' . $field . '">';
            $returnStr .= '<i class = "layui-icon">&#xe67c;</i>选择文件</button>';
            $returnStr .= '<div class="layui-upload-list" id="' . $field . '-list-div">';

            if ($data[$field]) {
                $picArray = explode("\n", $data[$field]); //0order 1url 2title 3default
                asort($picArray);
                foreach ($picArray as $value) {
                    $picInfoArray = explode('|', $value);
                    if (is_file(APP_ROOT . $picInfoArray[1]))
                        $returnStr .= '<li><a href="' . $picInfoArray[1] . '" target="_blank"><img class="layui-list-img" id="multipic-list-img" src="' . $picInfoArray[1] . '"></a><input type="hidden" name="multiUrl[]" value="' . $picInfoArray[1] . '"><input type="text" class="multiInputTitle" name="multiTitle[]" value="' . $picInfoArray[2] . '"><input type="text" class="multiInputOrder" name="multiOrder[]" value="' . $picInfoArray[0] . '"><input type="hidden" name="multiDefault[]" value="0"><span class="closed"></span></li>';
                }
            }

            $returnStr .= '</div></div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * input 时间字段
     * @param type $field
     * @return string|null
     */
    public function showCreated($field, $data, $menuData)
    {
        if (in_array($field, $menuData['allow_field'])) {
            $returnStr = '<div class="layui-form-item"><label class="layui-form-label">';
            $returnStr .= $menuData[$field];
            $returnStr .= '</label><div class="layui-input-block">';
            $returnStr .= '<input type="text" class="layui-input dataSelect" name="' . $field . '" id="' . $field . '" value="' . date('Y-m-d H:i:s', $data[$field] ? $data[$field] : NOW_TIME) . '" style="width:274px;" />';
            $returnStr .= '</div></div>';
            return $returnStr;
        }
        return NULL;
    }

    /**
     * input 字段
     * @param type $field
     * @return string|null
     */
    public function showAttach($field, $data, $menuData)
    {
        if (in_array($menuData['lm_id'], array(3, 4))) {
            $str1 = '出发地';
            $str2 = '目的地';
        } else {
            $str1 = '选择区域';
            $str2 = '选择区域';
        }
        $returnStr = '<tr><td width="100">';
        $returnStr .= $str1;
        $returnStr .= '</td><td class="textleft">';
        $returnStr .= '<input type="hidden" id="hfStarProvinceID" name="pro" value="' . $data['pro'] . '" />';
        $returnStr .= '<input type="hidden" id="hfStarCityID" name="city" value="' . $data['city'] . '" />';
        $returnStr .= '<input type="hidden" id="hfStarDistrictID" name="area" value="' . $data['area'] . '" />';
        $returnStr .= '<div class = "fl" id = "AtarSelectArea"></div>';
        $returnStr .= '</td></tr>';

        $returnStr1 = '<tr><td width="100">';
        $returnStr1 .= $str2;
        $returnStr1 .= '</td><td class="textleft">';
        $returnStr1 .= '<input type="hidden" id="hfStar1ProvinceID" name="pro1" value="' . $data['pro1'] . '" />';
        $returnStr1 .= '<input type="hidden" id="hfStar1CityID" name="city1" value="' . $data['city1'] . '" />';
        $returnStr1 .= '<input type="hidden" id="hfStar1DistrictID" name="area1" value="' . $data['area1'] . '" />';
        $returnStr1 .= '<div class = "fl" id = "AtarSelectArea1"></div>';
        $returnStr1 .= '</td></tr>';
        if (in_array($menuData['lm_id'], array(2, 5, 6, 7, 8, 9))) {
            return $returnStr;
        } elseif (in_array($menuData['lm_id'], array(3, 4))) {
            return $returnStr . $returnStr1;
        } else {
            return NULL;
        }
    }

}
